package com.ssfeng.youxia.services;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by liyuhang on 2019-09-03.
 */
@Service
public class PasswordService {

    private static final Logger logger = LoggerFactory.getLogger(PasswordService.class);

    @Autowired
    private EnvironmentService environmentService;

    /***
     * shiro加密
     * @param password
     * @return
     */
    public String encodeShiroPassword(String password) {
        String algorithm=environmentService.getProperty("algorithm");
        logger.debug("加密算法:{}",algorithm);
        //迭代次数
        String iterations=environmentService.getProperty("iterations");
        logger.debug("迭代次数:{}",iterations);
        //加盐
        String salt=environmentService.getProperty("salt");
        logger.debug("加盐:{}", salt);
        ByteSource credentialsSalt = ByteSource.Util.bytes(salt);
        SimpleHash hashPassword = new SimpleHash(algorithm, password, credentialsSalt, Integer.parseInt(iterations));
        return hashPassword.toHex();
    }
}
